//
//  ParsingXMLUtility.m
//  TestApp
//
//  Created by jwc on 2021/3/3.
//  Copyright © 2021 hyf. All rights reserved.
//

#import "ParsingXMLUtility.h"
#import "GDataXMLNode.h"

@implementation ParsingXMLUtility

/// 解析节点下的所有节点以及对应的值的字典数组
/// @param XMLString 要解析的XML
/// @param nodeString 要解析的节点 //RESPONSE/aa/bb
/// @param getStringNodes 要获取字符串的节点
+ (NSMutableArray *)parsingXML:(NSString *)XMLString NodeString:(NSString *)nodeString getStringNodes:(NSArray *)getStringNodes{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    if (XMLString == nil || [XMLString isEqualToString:@""] || XMLString.length == 0) {
        return list;
    }
    
    if (nodeString == nil || [nodeString isEqualToString:@""] || nodeString.length == 0) {
        return list;
    }
    
    GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithXMLString:XMLString options:0 error:nil];
    GDataXMLElement *root=[doc rootElement];
    //采用XPath方式解析xml
    NSArray *resultArray=[root nodesForXPath:nodeString error:nil];
    for (GDataXMLElement *item in resultArray) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSArray *itemChildren = [item children];
        for (GDataXMLElement *children in itemChildren) {
            NSString *name = [children name];
            NSString *value = @"";
            if ([getStringNodes containsObject:name]) {
                value = [children XMLString];
            }else{
                value = [children stringValue];
            }
            dic[name] = value;
        }
        [list addObject:dic];
    }
    return list;
}

/**
 *  @param XMLString 要解析的XML
 *  @param nodeString 要解析的节点 //RESPONSE/aa/bb
 *  @retrun NSMutableArray 解析节点下的所有的属性以及对应的值的字典数组
 */
+ (NSMutableArray *)parsingXMLAttr:(NSString *)XMLString NodeString:(NSString *)nodeString{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    if (XMLString == nil || [XMLString isEqualToString:@""] || XMLString.length == 0) {
        return list;
    }
    
    if (nodeString == nil || [nodeString isEqualToString:@""] || nodeString.length == 0) {
        return list;
    }

    GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithXMLString:XMLString options:0 error:nil];
    GDataXMLElement *root=[doc rootElement];
    //采用XPath方式解析xml
    NSArray *resultArray=[root nodesForXPath:nodeString error:nil];
    for (GDataXMLElement *item in resultArray) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSArray *itemAttrs = [item attributes];
        for (GDataXMLNode *attr in itemAttrs) {
            NSString *name = [attr name];
            NSString *value = [attr stringValue];
            dic[name] = value;
        }
        [list addObject:dic];
    }

    return list;
}

/// 删除xml节点
/// @param XMLString xml
/// @param nodeString 要解析的节点 //RESPONSE/aa/bb
/// @param stringNodes 要删除的节点
+(NSString *)delXMLNodeWithXML:(NSString *)XMLString NodeString:(NSString *)nodeString stringNodes:(NSArray *)stringNodes{
    if (XMLString.length == 0) {
        return XMLString;
    }
    GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithXMLString:XMLString options:0 error:nil];
    GDataXMLElement *root=[doc rootElement];
    //采用XPath方式解析xml
    NSArray *resultArray=[root nodesForXPath:nodeString error:nil];
    for (GDataXMLElement *item in resultArray) {
        NSArray *itemChildren = [item children];
        for (GDataXMLElement *children in itemChildren) {
            NSString *name = [children name];
            if([stringNodes containsObject:name]){
                [root removeChild:children];
            }
        }
    }
    return root.XMLString;
}



@end
